package com.examine.action;

import com.examine.bean.UploadFile;
import com.examine.utils.DateUtil;
import com.examine.utils.StrResponse;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.LinkedHashMap;

public class UploadJsonAction extends ActionSupport {
    //发送的json数据
    private LinkedHashMap mymp;
    //获取的文件
    private File file;
    //获取的用户ID
    private String userId;
    //Struts2自动获取的文件名字
    private String fileFileName;
    //Struts2自动获取的文件类型
    private String fileContentType;

    public LinkedHashMap getModelMap() {
        return mymp;
    }
    public void setModelMap(LinkedHashMap mymp) {
        this.mymp = mymp;
    }

    public File getFile() {
        return file;
    }
    public void setFile(File file) {
        this.file = file;
    }

    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getFileContentType() {
        return fileContentType;
    }
    public void setFileContentType(String fileContentType) {
        this.fileContentType = fileContentType;
    }

    public String getFileName() {
        return fileFileName;
    }
    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

    private String path = "approvalProcess" + File.separator + "files";

    public String uploadFile(){

        //判断用户的id信息是否为空
        if(StringUtils.isEmpty(getUserId())){
            mymp = StrResponse.error("用户ID不能为空");
            return ERROR;
        }

        //判断用户的文件数据是否为空
        if(getFile() == null){
            mymp = StrResponse.error("上传的文件不能为空");
            return ERROR;
        }

        System.out.println(getFileName());
        System.out.println(getFileContentType());

        //读取工作目录
        String realPath = ServletActionContext.getServletContext().getRealPath("/");
        //将图片存入到系统专属目录中
        String filePath = realPath + path + File.separator + getUserId();

        //判断文件是否存在，不存在就创建一个
        File targetFile = new File(filePath);
        if(!targetFile.exists()){
            targetFile.mkdirs();
        }

        //获取到图片的存储路径返回给用户，以便保存到数据库进行访问
        String urlPath = filePath + File.separator + getFileName();

        //创建本地实体将基本信息包括图片的存放路径返回给用户
        UploadFile uploadFile = new UploadFile();
        uploadFile.setUserId(getUserId());
        uploadFile.setPictureTitle(getFileName());
        uploadFile.setPictureUrl(path + File.separator + getUserId() + File.separator + getFileName());
        uploadFile.setCreateTime(DateUtil.getCurrentTime());

        //文件的写入与保存
        try {
            FileInputStream fis = new FileInputStream(getFile());
            FileOutputStream out = new FileOutputStream(urlPath);
            byte[] buffer = new byte[1024];
            int len;
            while( (len = fis.read(buffer)) > 0){
                out.write(buffer, 0, len);
            }
            //关闭数据传输流
            fis.close();
            out.flush();
            out.close();

            //将上传成功的信息返回给用户
            mymp = StrResponse.success("图片上传成功", uploadFile);
            return SUCCESS;

        } catch (Exception e) {
            e.printStackTrace();
            //发生数据的写入错误时，给用户提示信息
            mymp = StrResponse.error("操作失败");
            return ERROR;
        }

    }


}
